package ee.greaton.SMSsender;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ReceivedDBAdapter {

	public static final String KEY_FROM = "from";
	public static final String KEY_CONTENT = "content";
	public static final String KEY_DATE = "date";
	public static final String KEY_TIME = "time";
	private static final String DATABASE_TABLE = "received";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + "(from text not null,"
			+ " content text not null, date text not null, time text not null);";
	private DatabaseHelper DBHelper;
	SQLiteDatabase db;

	public ReceivedDBAdapter(Context context) {
		DBHelper = new DatabaseHelper(context, DATABASE_TABLE,
				DATABASE_VERSION, DATABASE_CREATE);
	}

	public ReceivedDBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		DBHelper.close();
	}
	
	public void addMessage(String from, String content, String date, String time){
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_FROM, from);
		initialValues.put(KEY_CONTENT, content);
		initialValues.put(KEY_DATE, date);
		initialValues.put(KEY_TIME, time);
		db.insert(DATABASE_TABLE, null, initialValues);
	}

	public int getCount() {
		Cursor cursor = getMessages();
		int count = cursor.getCount();
		cursor.close();
		return count;
	}

	public Cursor getMessages() {
		String[] a = {KEY_FROM, KEY_CONTENT, KEY_DATE, KEY_TIME};
		return db.query(DATABASE_TABLE, a, null, null, null, null, null);
	}
}
